package com.chuangshu.orange.controller;

import com.chuangshu.orange.entity.Comment;
import com.chuangshu.orange.oss.service.AliOssService;
import com.chuangshu.orange.result.Result;
import com.chuangshu.orange.result.ResultUtil;
import com.chuangshu.orange.service.CommentService;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * (Comment)表控制层
 *
 * @author makejava
 * @since 2021-03-21 17:00:02
 */
@RestController
@RequestMapping("comment")
@Api(tags = {"评论"})
public class CommentController {
    /**
     * 服务对象
     */
    @Resource
    private CommentService commentService;
    @Resource
    private AliOssService aliOssService;


    //增加评论
    @ApiOperation(value = "增加一条评论")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "newsId", dataType = "int", required = true, value = "新闻编号"),
            @ApiImplicitParam(paramType = "query", name = "context", dataType = "String", required = true, value = "评论内容"),
            @ApiImplicitParam(paramType = "query", name = "name", dataType = "String", required = true, value = "用户名字")

    })
    @PostMapping("insOne")
    public Result<Object> insOne(Integer newsId, String context, String name, @ApiParam(value = "用户头像")MultipartFile touxiangUrl){
        if(newsId ==null||context==null||name==null||touxiangUrl==null){
            return ResultUtil.Error("404","参数不能为空",null);
        }
        //上传图片并返回文件路径
        String upload = aliOssService.upload(touxiangUrl);
        if(upload==null){
            return ResultUtil.Error("500","图片上传失败",null);
        }
        Comment comment = new Comment();
        comment.setContext(context);
        comment.setCreateTime(new Date());
        comment.setDianzanNum(0);
        comment.setNewsId(newsId);
        comment.setTouxiangUrl(upload);
        comment.setName(name);
        Comment insert = this.commentService.insert(comment);
        return ResultUtil.Success(insert);
    }
    //查询指定新闻的所有评论
    @ApiOperation(value = "查询指定新闻的所有评论")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "newsId", dataType = "int", required = true, value = "新闻编号")
    })
    @GetMapping("selOnlyAll")
    public Result<Object> selOnlyAll(Integer newsId){
        if(newsId == null){
            return ResultUtil.Error("404","参数不能为空",null);
        }
        Comment comment = new Comment();
        comment.setNewsId(newsId);
        List<Comment> comments = this.commentService.queryAll(comment);
        return ResultUtil.Success(comments);
    }
    //点赞
    @ApiOperation(value = "给指定评论点赞")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", dataType = "int", required = true, value = "评论编号"),
    })
    @PutMapping("upOne")
    public Result<Object> upOne(Integer id){
        //根据编号查询新闻
        Comment comment = commentService.queryById(id);
        //将点赞数加1

        comment.setDianzanNum(comment.getDianzanNum()+1);
        //更新数据库
        Comment update = commentService.update(comment);
        //返回
        return ResultUtil.Success(update);
    }
    //取消点赞
    @ApiOperation(value = "给指定评论取消点赞")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", dataType = "int", required = true, value = "评论编号"),
    })
    @PutMapping("downOne")
    public Result<Object> downOne(Integer id){
        //根据编号查询新闻
        Comment comment = commentService.queryById(id);
        //将点赞数加1
        comment.setDianzanNum(comment.getDianzanNum()-1);
        //更新数据库
        Comment update = commentService.update(comment);
        //返回
        return ResultUtil.Success(update);
    }
}